<?php
if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
if ($curruser['acl'] != "5")
{
    exit();
}
$conf = new Config();
$lotto_id = intval($_GET['id']);
$bettype = $_GET['bettype'];
$numbers = $_GET['number'];
$rate = $_GET['rate'];
$order_id = isset($_GET['order']) ? $_GET['order'] : 0;
$curruser = User::getuser();
$userid = $curruser['id'];
$debug = false;
if ($debug)
{
    $fp = fopen("test.log", "a+");
    fwrite($fp, "id={$lotto_id}, bettype={$bettype}, numbers={$numbers}, rate={$rate}, order={$order_id}\n");
    fclose($fp);
}

if ($bettype == "A" || $bettype == "B" || $bettype == "C")
{
    $table = $bettype == "A" ? "rate0s" : "rate4s";
    if ($bettype == "C")
    {
        switch ($order_id)
        {
            case 1 :
                $table = "rate41s";
                break;
            case 2 :
                $table = "rate42s";
                break;
            case 3 :
                $table = "rate43s";
                break;
            case 4 :
                $table = "rate44s";
                break;
            case 5 :
                $table = "rate45s";
                break;
            case 6 :
                $table = "rate46s";
                break;
            default :
                exit();
                break;
        }
    }
    $rate = floatval($rate);
    switch ($numbers)
    {
        case 11 :
            foreach ($g_red_po as $n)
            {
                $n2 = d2($n);
                if (0 < $rate)
                {
                    getdb("update " . $table . " set r" . $n2 . " = r" . $n2 . " + " . $rate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                }
                else if ($rate < 0)
                {
                    getdb("update " . $table . " set r" . $n2 . " = 0 where lotto_id=" . $lotto_id . " and user_id=" . $userid . " and user_id=" . $userid);
                }
            }
            break;
        case 12 :
            foreach ($g_blue_po as $n)
            {
                $n2 = d2($n);
                if (0 < $rate)
                {
                    getdb("update " . $table . " set r" . $n2 . " = r" . $n2 . " + " . $rate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                }
                else if ($rate < 0)
                {
                    getdb("update " . $table . " set r" . $n2 . " = 0 where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                }
            }
            break;
        case 13 :
            foreach ($g_green_po as $n)
            {
                $n2 = d2($n);
                if (0 < $rate)
                {
                    getdb("update " . $table . " set r" . $n2 . " = r" . $n2 . " + " . $rate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                }
                else if ($rate < 0)
                {
                    getdb("update " . $table . " set r" . $n2 . " = 0 where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                }
            }
            break;
        case 14 :
            $i = 1;
            while ($i < 49)
            {
                if ($i % 2 == 1)
                {
                    $n2 = d2($i);
                    if (0 < $rate)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = r" . $n2 . " + " . $rate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                    }
                    else if ($rate < 0)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = 0 where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                    }
                }
                ++$i;
            }
            break;
        case 15 :
            $i = 1;
            while ($i < 49)
            {
                if ($i % 2 == 0)
                {
                    $n2 = d2($i);
                    if (0 < $rate)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = r" . $n2 . " + " . $rate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                    }
                    else if ($rate < 0)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = 0 where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                    }
                }
                ++$i;
            }
            break;
        case 16 :
            $i = 25;
            while ($i < 49)
            {
                $n2 = d2($i);
                if (0 < $rate)
                {
                    getdb("update " . $table . " set r" . $n2 . " = r" . $n2 . " + " . $rate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                }
                else if ($rate < 0)
                {
                    getdb("update " . $table . " set r" . $n2 . " = 0 where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                }
                ++$i;
            }
            break;
        case 17 :
            $i = 1;
            while ($i < 25)
            {
                $n2 = d2($i);
                if (0 < $rate)
                {
                    getdb("update " . $table . " set r" . $n2 . " = r" . $n2 . " + " . $rate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                }
                else if ($rate < 0)
                {
                    getdb("update " . $table . " set r" . $n2 . " = 0 where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                }
                ++$i;
            }
            break;
        case 18 :
            foreach ($g_sumnum_odd as $i)
            {
                $n2 = d2($i);
                if (0 < $rate)
                {
                    getdb("update " . $table . " set r" . $n2 . " = r" . $n2 . " + " . $rate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                }
                else if ($rate < 0)
                {
                    getdb("update " . $table . " set r" . $n2 . " = 0 where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                }
            }
            break;
        case 19 :
            foreach ($g_sumnum_even as $i)
            {
                $n2 = d2($i);
                if (0 < $rate)
                {
                    getdb("update " . $table . " set r" . $n2 . " = r" . $n2 . " + " . $rate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                }
                else if ($rate < 0)
                {
                    getdb("update " . $table . " set r" . $n2 . " = 0 where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                }
            }
            break;
        case 20 :
            $i = 1;
            while ($i < 50)
            {
                $n2 = d2($i);
                if (0 < $rate)
                {
                    getdb("update " . $table . " set r" . $n2 . " = r" . $n2 . " + " . $rate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                }
                else if ($rate < 0)
                {
                    getdb("update " . $table . " set r" . $n2 . " = 0 where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                }
                ++$i;
            }
            break;
        case 21 :
            $top10 = split(",", $_GET['top10']);
            foreach ($top10 as $i)
            {
                $i = intval($i);
                if (0 < $i)
                {
                    $n2 = d2($i);
                    if (0 < $rate)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = r" . $n2 . " + " . $rate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                    }
                    else if ($rate < 0)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = 0 where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                    }
                }
            }
            break;
        case 22 :
            foreach ($g_sumnum_dom as $i)
            {
                $n2 = d2($i);
                if (0 < $rate)
                {
                    getdb("update " . $table . " set r" . $n2 . " = r" . $n2 . " + " . $rate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                }
                else if ($rate < 0)
                {
                    getdb("update " . $table . " set r" . $n2 . " = 0 where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                }
            }
            break;
        case 23 :
            foreach ($g_sumnum_wild as $i)
            {
                $n2 = d2($i);
                if (0 < $rate)
                {
                    getdb("update " . $table . " set r" . $n2 . " = r" . $n2 . " + " . $rate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                }
                else if ($rate < 0)
                {
                    getdb("update " . $table . " set r" . $n2 . " = 0 where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                }
            }
            break;
        case 24 :
            $top05 = split(",", $_GET['top05']);
            foreach ($top05 as $i)
            {
                $i = intval($i);
                if (0 < $i)
                {
                    $n2 = d2($i);
                    if (0 < $rate)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = r" . $n2 . " + " . $rate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                    }
                    else if ($rate < 0)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = 0 where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                    }
                }
            }
            break;
        case 25 :
            foreach ($g_red_po as $n)
            {
                if ($n % 2 != 0)
                {
                    $n2 = d2($n);
                    if (0 < $rate)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = r" . $n2 . " + " . $rate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                    }
                    else if ($rate < 0)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = 0 where lotto_id=" . $lotto_id . " and user_id=" . $userid . " and user_id=" . $userid);
                    }
                }
            }
            break;
        case 26 :
            foreach ($g_red_po as $n)
            {
                if ($n % 2 == 0)
                {
                    $n2 = d2($n);
                    if (0 < $rate)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = r" . $n2 . " + " . $rate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                    }
                    else if ($rate < 0)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = 0 where lotto_id=" . $lotto_id . " and user_id=" . $userid . " and user_id=" . $userid);
                    }
                }
            }
            break;
        case 27 :
            foreach ($g_red_po as $n)
            {
                if (24 < $n)
                {
                    $n2 = d2($n);
                    if (0 < $rate)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = r" . $n2 . " + " . $rate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                    }
                    else if ($rate < 0)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = 0 where lotto_id=" . $lotto_id . " and user_id=" . $userid . " and user_id=" . $userid);
                    }
                }
            }
            break;
        case 28 :
            foreach ($g_red_po as $n)
            {
                if ($n < 25)
                {
                    $n2 = d2($n);
                    if (0 < $rate)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = r" . $n2 . " + " . $rate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                    }
                    else if ($rate < 0)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = 0 where lotto_id=" . $lotto_id . " and user_id=" . $userid . " and user_id=" . $userid);
                    }
                }
            }
            break;
        case 29 :
            foreach ($g_blue_po as $n)
            {
                if ($n % 2 != 0)
                {
                    $n2 = d2($n);
                    if (0 < $rate)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = r" . $n2 . " + " . $rate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                    }
                    else if ($rate < 0)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = 0 where lotto_id=" . $lotto_id . " and user_id=" . $userid . " and user_id=" . $userid);
                    }
                }
            }
            break;
        case 30 :
            foreach ($g_blue_po as $n)
            {
                if ($n % 2 == 0)
                {
                    $n2 = d2($n);
                    if (0 < $rate)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = r" . $n2 . " + " . $rate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                    }
                    else if ($rate < 0)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = 0 where lotto_id=" . $lotto_id . " and user_id=" . $userid . " and user_id=" . $userid);
                    }
                }
            }
            break;
        case 31 :
            foreach ($g_blue_po as $n)
            {
                if (24 < $n)
                {
                    $n2 = d2($n);
                    if (0 < $rate)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = r" . $n2 . " + " . $rate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                    }
                    else if ($rate < 0)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = 0 where lotto_id=" . $lotto_id . " and user_id=" . $userid . " and user_id=" . $userid);
                    }
                }
            }
            break;
        case 32 :
            foreach ($g_blue_po as $n)
            {
                if ($n < 25)
                {
                    $n2 = d2($n);
                    if (0 < $rate)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = r" . $n2 . " + " . $rate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                    }
                    else if ($rate < 0)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = 0 where lotto_id=" . $lotto_id . " and user_id=" . $userid . " and user_id=" . $userid);
                    }
                }
            }
            break;
        case 33 :
            foreach ($g_green_po as $n)
            {
                if ($n % 2 != 0)
                {
                    $n2 = d2($n);
                    if (0 < $rate)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = r" . $n2 . " + " . $rate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                    }
                    else if ($rate < 0)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = 0 where lotto_id=" . $lotto_id . " and user_id=" . $userid . " and user_id=" . $userid);
                    }
                }
            }
            break;
        case 34 :
            foreach ($g_green_po as $n)
            {
                if ($n % 2 == 0)
                {
                    $n2 = d2($n);
                    if (0 < $rate)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = r" . $n2 . " + " . $rate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                    }
                    else if ($rate < 0)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = 0 where lotto_id=" . $lotto_id . " and user_id=" . $userid . " and user_id=" . $userid);
                    }
                }
            }
            break;
        case 35 :
            foreach ($g_green_po as $n)
            {
                if (24 < $n)
                {
                    $n2 = d2($n);
                    if (0 < $rate)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = r" . $n2 . " + " . $rate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                    }
                    else if ($rate < 0)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = 0 where lotto_id=" . $lotto_id . " and user_id=" . $userid . " and user_id=" . $userid);
                    }
                }
            }
            break;
        case 36 :
            foreach ($g_green_po as $n)
            {
                if ($n < 25)
                {
                    $n2 = d2($n);
                    if (0 < $rate)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = r" . $n2 . " + " . $rate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                    }
                    else if ($rate < 0)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = 0 where lotto_id=" . $lotto_id . " and user_id=" . $userid . " and user_id=" . $userid);
                    }
                }
            }
            break;
        case 37 :
            $i = 1;
            while ($i < 49)
            {
                $n2 = d2($i);
                $ss = substr($n2, 0 - 1);
                if (4 < $ss)
                {
                    if (0 < $rate)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = r" . $n2 . " + " . $rate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                    }
                    else if ($rate < 0)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = 0 where lotto_id=" . $lotto_id . " and user_id=" . $userid . " and user_id=" . $userid);
                    }
                }
                ++$i;
            }
            break;
        case 38 :
            $i = 1;
            while ($i < 49)
            {
                $n2 = d2($i);
                $ss = substr($n2, 0 - 1);
                if ($ss < 5)
                {
                    if (0 < $rate)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = r" . $n2 . " + " . $rate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
                    }
                    else if ($rate < 0)
                    {
                        getdb("update " . $table . " set r" . $n2 . " = 0 where lotto_id=" . $lotto_id . " and user_id=" . $userid . " and user_id=" . $userid);
                    }
                }
                ++$i;
            }
            break;
    }
}
else
{
    $bettype = intval($bettype);
    $arRs = getdb("SELECT * FROM autorate_config_6h WHERE bettype_id={$bettype} AND subid=0 LIMIT 1");
    switch ($bettype)
    {
        case 0 :
        case 24 :
            if (0 < $rate)
            {
                getdb("update rate0s set r" . $numbers . "=r" . $numbers . "+" . $rate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
            }
            else if ($rate < 0)
            {
                getdb("update rate0s set r" . $numbers . "=0 where lotto_id=" . $lotto_id . " and user_id=" . $userid);
            }
            break;
        case 4 :
        case 31 :
            if (0 < $rate)
            {
                getdb("update rate4s set r" . $numbers . "=r" . $numbers . "+" . $rate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
            }
            else if ($rate < 0)
            {
                getdb("update rate4s set r" . $numbers . "=0 where lotto_id=" . $lotto_id . " and user_id=" . $userid);
            }
            break;
        case 1 :
        case 2 :
        case 3 :
        case 20 :
        case 28 :
        case 30 :
        case 35 :
        case 47 :
            $newrated = $arRs->f['downrate1'];
            $n1 = "r" . $bettype . "_1";
            if ($numbers == 2)
            {
                $n1 = "r" . $bettype . "_2";
            }
            if (0 < $rate)
            {
                $newrated = $newrated * $rate;
                getdb("update rate1s set " . $n1 . "=" . $n1 . "+" . $newrated . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
            }
            else if ($rate < 0)
            {
                getdb("update rate1s set " . $n1 . "=0 where lotto_id=" . $lotto_id . " and user_id=" . $userid);
            }
            break;
        case 5 :
        case 6 :
            $newrated = $arRs->f['downrate1'];
            $n1 = "r" . $bettype . "_1";
            $n2 = "r" . $bettype . "_2";
            if ($numbers == 2)
            {
                $n1 = "r" . $bettype . "_2";
                $n2 = "r" . $bettype . "_1";
            }
            if (0 < $rate)
            {
                $newrated = $newrated * $rate;
                getdb("update rate5s set " . $n1 . "=" . $n1 . "+" . $newrated . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
            }
            else if ($rate < 0)
            {
                getdb("update rate5s set " . $n1 . "=0 where lotto_id=" . $lotto_id . " and user_id=" . $userid);
            }
            break;
        case 13 :
        case 14 :
        case 21 :
        case 22 :
        case 34 :
        case 18 :
        case 46 :
            $newrate = $arRs->f['downrate1'];
            $n1 = "r" . $bettype . "_" . $numbers;
            if (0 < $rate)
            {
                $newrate = $newrate * $rate;
                getdb("update rate1s set " . $n1 . "=" . $n1 . "+" . $newrate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
            }
            else if ($rate < 0)
            {
                getdb("update rate1s set " . $n1 . "=0 where lotto_id=" . $lotto_id . " and user_id=" . $userid);
            }
            break;
		case 49 :
		case 50 :
			$newrate = $arRs->f['downrate1'];
            $n1 = "r". ($numbers-1);
            if (0 < $rate)
            {
                $newrate = $newrate * $rate;
                getdb("update rate37s set " . $n1 . "=" . $n1 . "+" . $newrate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
            }
            else if ($rate < 0)
            {
                getdb("update rate37s set " . $n1 . "=0 where lotto_id=" . $lotto_id . " and user_id=" . $userid);
            }
            break; 	
        case 19 :
            $newrate = $arRs->f['downrate1'];
            $newrate = 0 - $newrate * $rate;
            $subid1 = substr($numbers, 0, 1);
            $n0 = substr($numbers, 0 - 3);
            $n1 = "r19_{$subid1}" . $n0;
            if (0 < $rate)
            {
                getdb("update rate1s set " . $n1 . "=" . $n1 . "-" . $newrate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
            }
            else if ($rate < 0)
            {
                getdb("update rate1s set " . $n1 . "=0 where lotto_id=" . $lotto_id . " and user_id=" . $userid);
            }
            break;
        case 25 :
        case 26 :
            if ($order_id < 1 || 6 < $order_id)
            {
                exit();
            }
            if (0 < $rate)
            {
                getdb("update rate4" . $order_id . "s set r" . $numbers . "=r" . $numbers . "+" . $rate . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
            }
            else if ($rate < 0)
            {
                getdb("update rate4" . $order_id . "s set r" . $numbers . "=0 where lotto_id=" . $lotto_id . " and user_id=" . $userid);
            }
            break;
        case 23 :
            $nn = split(" ", $numbers);
            $num1 = $nn[0];
            $num2 = $nn[1];
            $n1 = "r15_" . $num1 . "_" . $num2;
            $newrated = 0;
            echo $n1 . "<br>";
            switch ($num2)
            {
                case 1 :
                case 2 :
                case 8 :
                case 9 :
                    $newrated = $arRs->f['downrate1'];
                    break;
                case 3 :
                case 4 :
                    $newrated = $arRs->f['downrate1'];
                    break;
                case 5 :
                case 6 :
                case 7 :
                    $newrated = $arRs->f['downrate1'];
                    $newrated = $newrated * $rate;
                    break;
            }
            echo "new rate : " . $newrated;
            if (0 < $rate)
            {
                getdb("update rate15s set " . $n1 . "=" . $n1 . "+" . $newrated . " where lotto_id=" . $lotto_id . " and user_id=" . $userid);
            }
            else
            {
                getdb("update rate15s set " . $n1 . "=0 where lotto_id=" . $lotto_id . " and user_id=" . $userid);
            }
            break;
    }
}
?>